{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "22592fb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pyecharts in e:\\python311\\lib\\site-packages (2.0.3)\n",
      "Requirement already satisfied: jinja2 in e:\\python311\\lib\\site-packages (from pyecharts) (3.1.2)\n",
      "Requirement already satisfied: prettytable in e:\\python311\\lib\\site-packages (from pyecharts) (3.7.0)\n",
      "Requirement already satisfied: simplejson in e:\\python311\\lib\\site-packages (from pyecharts) (3.19.1)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in e:\\python311\\lib\\site-packages (from jinja2->pyecharts) (2.1.2)\n",
      "Requirement already satisfied: wcwidth in e:\\python311\\lib\\site-packages (from prettytable->pyecharts) (0.2.6)\n"
     ]
    }
   ],
   "source": [
    "!pip install pyecharts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff09701f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app '__main__'\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\n",
      " * Running on http://127.0.0.1:5000\n",
      "Press CTRL+C to quit\n",
      "[2023-12-20 18:55:14,989] ERROR in app: Exception on / [GET]\n",
      "Traceback (most recent call last):\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\app.py\", line 2528, in wsgi_app\n",
      "    response = self.full_dispatch_request()\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\app.py\", line 1825, in full_dispatch_request\n",
      "    rv = self.handle_user_exception(e)\n",
      "         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\app.py\", line 1823, in full_dispatch_request\n",
      "    rv = self.dispatch_request()\n",
      "         ^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\app.py\", line 1799, in dispatch_request\n",
      "    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\86130\\AppData\\Local\\Temp\\ipykernel_20500\\2682670213.py\", line 41, in index\n",
      "    return render_template(\"index.html\")\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\templating.py\", line 146, in render_template\n",
      "    template = app.jinja_env.get_or_select_template(template_name_or_list)\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\jinja2\\environment.py\", line 1081, in get_or_select_template\n",
      "    return self.get_template(template_name_or_list, parent, globals)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\jinja2\\environment.py\", line 1010, in get_template\n",
      "    return self._load_template(name, globals)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\jinja2\\environment.py\", line 969, in _load_template\n",
      "    template = self.loader.load(self, name, self.make_globals(globals))\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\jinja2\\loaders.py\", line 126, in load\n",
      "    source, filename, uptodate = self.get_source(environment, name)\n",
      "                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\templating.py\", line 62, in get_source\n",
      "    return self._get_source_fast(environment, template)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\templating.py\", line 98, in _get_source_fast\n",
      "    raise TemplateNotFound(template)\n",
      "jinja2.exceptions.TemplateNotFound: index.html\n",
      "127.0.0.1 - - [20/Dec/2023 18:55:15] \"GET / HTTP/1.1\" 500 -\n",
      "127.0.0.1 - - [20/Dec/2023 18:55:15] \"GET /favicon.ico HTTP/1.1\" 404 -\n",
      "[2023-12-20 18:55:26,585] ERROR in app: Exception on / [GET]\n",
      "Traceback (most recent call last):\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\app.py\", line 2528, in wsgi_app\n",
      "    response = self.full_dispatch_request()\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\app.py\", line 1825, in full_dispatch_request\n",
      "    rv = self.handle_user_exception(e)\n",
      "         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\app.py\", line 1823, in full_dispatch_request\n",
      "    rv = self.dispatch_request()\n",
      "         ^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\app.py\", line 1799, in dispatch_request\n",
      "    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\86130\\AppData\\Local\\Temp\\ipykernel_20500\\2682670213.py\", line 41, in index\n",
      "    return render_template(\"index.html\")\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\templating.py\", line 146, in render_template\n",
      "    template = app.jinja_env.get_or_select_template(template_name_or_list)\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\jinja2\\environment.py\", line 1081, in get_or_select_template\n",
      "    return self.get_template(template_name_or_list, parent, globals)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\jinja2\\environment.py\", line 1010, in get_template\n",
      "    return self._load_template(name, globals)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\jinja2\\environment.py\", line 969, in _load_template\n",
      "    template = self.loader.load(self, name, self.make_globals(globals))\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\jinja2\\loaders.py\", line 126, in load\n",
      "    source, filename, uptodate = self.get_source(environment, name)\n",
      "                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\templating.py\", line 62, in get_source\n",
      "    return self._get_source_fast(environment, template)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"E:\\Python311\\Lib\\site-packages\\flask\\templating.py\", line 98, in _get_source_fast\n",
      "    raise TemplateNotFound(template)\n",
      "jinja2.exceptions.TemplateNotFound: index.html\n",
      "127.0.0.1 - - [20/Dec/2023 18:55:26] \"GET / HTTP/1.1\" 500 -\n"
     ]
    }
   ],
   "source": [
    "from random import randrange\n",
    "\n",
    "from flask import Flask, render_template\n",
    "\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar\n",
    "from pyecharts.faker import Faker\n",
    "from pyecharts.globals import ChartType\n",
    "\n",
    "app = Flask(__name__, static_folder=\"templates\")\n",
    "\n",
    "\n",
    "def bar_base() -> Bar:\n",
    "    c = (\n",
    "        Bar()\n",
    "        .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "        .add_yaxis(\"商家A\", [randrange(0, 100) for _ in range(6)])\n",
    "        .add_yaxis(\"商家B\", [randrange(0, 100) for _ in range(6)])\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=\"Bar-基本示例\", subtitle=\"我是副标题\"))\n",
    "    )\n",
    "    return c\n",
    "\n",
    "def geo_lines():\n",
    "    c = (\n",
    "        Geo()\n",
    "        .add_schema(maptype=\"广东\")\n",
    "        .add(\n",
    "            \"geo\",\n",
    "            [list(z) for z in zip(Faker.guangdong_city, Faker.values())],\n",
    "            type_=ChartType.HEATMAP,\n",
    "        )\n",
    "        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "        .set_global_opts(\n",
    "            visualmap_opts=opts.VisualMapOpts(), title_opts=opts.TitleOpts(title=\"Geo-广东地图\")\n",
    "        )\n",
    "    )\n",
    "    return c\n",
    "\n",
    "@app.route(\"/\")\n",
    "def index():\n",
    "    return render_template(\"index.html\")\n",
    "\n",
    "\n",
    "@app.route(\"/barChart\")\n",
    "def get_bar_chart():\n",
    "    c = bar_base()\n",
    "    return c.dump_options_with_quotes()\n",
    "\n",
    "@app.route(\"/geo_lines\")\n",
    "def get_geo_lines():\n",
    "    c = geo_lines()\n",
    "    return c.dump_options_with_quotes()\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f115133",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
